var $$ = mdui.JQ;
const URL = 'codes/list.json.txt';

function initCodeDrawerList() {
    $$.ajax({
        method: 'GET',
        url: URL,
        success: function (data) {
            // console.log(data);
            createCodeListDOM(JSON.parse(data));
        },
        error: function (XHR, msg) {
            // console.log(XHR);
            $$('#drawer-codelist li').show();
        }
    })
}

function createCodeListDOM(arrData) {
    var dom = $$('#drawer-codelist');
    $$(dom).empty();
    arrData.forEach(item => {
        $$(dom).append(
            '<a href="javascript:;" class="mdui-list-item mdui-ripple"' + 
            ' data-url="' + item.url + 
            '" data-img="' + item.img + 
            '" data-description="' + item.description + 
            '">' + item.title + '</a>'
        )
    });
    $$(dom).children().on('click', function(){
        refreshPageDemo(
            $$(this).attr('data-img'), 
            $$(this).attr('data-url'), 
            $$(this).attr('data-description')
        );
    });
}

function refreshPageDemo(img, url, description) {
    $$('#page-demo img').attr('src', img);
    $$('#page-demo iframe').attr('src', url);
    $$('#page-demo pre').attr('data-src', url);
    $$('#page-description p').text(description);
    Prism.fileHighlight();
}

function resizePageDemoIframe() {
    var iframe = document.getElementById('page-demo-iframe');
    var iframeWin = iframe.contentWindow || iframe.contentDocument.parentWindow;
    if (iframeWin.document.body) {
        iframe.height = iframeWin.document.documentElement.scrollHeight || iframeWin.document.body.scrollHeight;
    }
}